package com.extweb4j.web.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import com.extweb4j.core.bean.Msg;
import com.extweb4j.core.comstant.CacheConstant;
import com.extweb4j.core.controller.ExtController;
import com.extweb4j.core.daoaload.ActionAuthDataLoader;
import com.extweb4j.core.enums.RowStatus;
import com.extweb4j.core.kit.CaptchaKit;
import com.extweb4j.core.kit.ExtKit;
import com.extweb4j.core.kit.LoginKit;
import com.extweb4j.core.model.ExtMenu;
import com.extweb4j.core.model.ExtUser;
import com.jfinal.aop.Clear;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.upload.UploadFile;

@Clear
public class IndexController extends ExtController{
	/**
	 * 导航页
	 */
	public void index(){
		
	}
	/**
	 * 登录导航页
	 */
	public void login(){
		redirect("/");
	}
	/**
	 * 执行登录
	 */
	public void doLogin(){
	    try {
			LoginKit.login(getRequest(),getResponse());
			success();
		} catch (Exception e) {
			renderJson(new Msg(false, ExtKit.formatException(e)));
		}
	}
	/**
	 * 小程序登陆接口
	 * 返回uid
	 */
	public void xcxLogin(){
		try{
			String userId  = getPara("userid");
			String password  = getPara("password");
			if(StringUtils.isBlank(userId) || StringUtils.isBlank(password)){
				throw new RuntimeException("用户名,密码不能为空");
			}
			ExtUser user = ExtUser.dao.findUserBy(userId,ExtKit.MD5(password));		
			if(user == null){	
				throw new RuntimeException("用户名或密码错误");	
			}else{	
				if(user.getStr("row_status") .equals( RowStatus.启用.getState() )){
					setCookie(new Cookie("uid",user.getId()));
					renderJson(user);
				}else{
					throw new RuntimeException("您的账号已被锁定,请联系超级管理员");
				}
			
			}	
		}catch(Exception e){
			renderJson(new Msg(false, ExtKit.formatException(e)));
		}
		
	}
	/**
	 * 登出控制器
	 */
	public void logout(){
		LoginKit.logout(getRequest());
		redirect("/");
	}
	/**
	 * 验证是否登录
	 */
	public void isLogin(){
		renderJson(new Msg(LoginKit.isLogin(getRequest(),getResponse())));
	}
	/**
	 * 验证码
	 */
	public void captcha(){
		
		HttpServletResponse response = getResponse();
		try {
			String verifyCode = CaptchaKit.outputImage(response.getOutputStream());
			setSessionAttr("captcha",verifyCode );
			System.out.println("验证码:" + verifyCode);
		} catch (IOException e) {
			e.printStackTrace();
		}
		renderNull();
		
	}
	
	/**
	 * 获取当前用户
	 */
	
	public void getUser(){
		renderJson(new Msg(true, "OK",LoginKit.getSessionUser(getRequest())));
	}
	
	/**
	 * wangHtml 上传图片
	 */
	@Deprecated
	public void uploadImg(){
		UploadFile uploadFile = getFile();
		try {
			String path = PropKit.get("uploadImg");
			FileUtils.copyFile(uploadFile.getFile(),new File(path+uploadFile.getFileName()));
			renderText("https://www.baidu.com/img/bd_logo1.png");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			renderText("error|服务器端错误");
		}
	}
	/**
	 * 验证改用户是否有权限
	 */
	public void hasAuth(){

		String action = getPara("action");
		String uid = getCookie("uid");
		if(uid==null){
			throw new RuntimeException("登录过期,请重新登录");
		}
		
		List<String> auths = new ArrayList<String>();
		List<ExtMenu> list = CacheKit.get(CacheConstant.GLOBAL_CACHE_NAME,(uid+"_action"), new ActionAuthDataLoader(uid));
		for(ExtMenu m : list){
			auths.add(m.getStr("action"));
		}
		if(!auths.contains(action)){
			throw new RuntimeException("没有权限,"+action);
		}
		success();
	}
	
}
